﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IllustratedCsharp7_ch19_EnumeratorsAndIterators
{
    class ColorEnumrator : IEnumerator
    {
        string[] colors;
        int position = -1;
        string[] _colors
        {
            get { return colors; }
            set {colors = value; }
        }

        public ColorEnumrator(string[] theColors)
        {
            colors = new string[theColors.Length];
            for (int i = 0; i < theColors.Length; i++)
                colors[i] = theColors[i];
        }
        public object Current 
        {
            get
            {
                if (position == -1)
                    throw new InvalidOperationException();
                if(position >= colors.Length)
                    throw new InvalidOperationException();
                return colors[position];
            }    
        }

        public bool MoveNext()
        {
            if (position < colors.Length - 1)
            {
                position++;
                return true;
            }
            else
                return false;
        }

        public void Reset()
        {
            position = -1;
        }
    }
    class Spectrun : IEnumerable
    {
        string[] Colors = { "violet", "blue", "cyan", "green", "yelloe", "orange", "red" };
        public IEnumerator GetEnumerator()
        {
            return new ColorEnumrator(Colors);
        }
    }
    class Program
    {
       
        static void _Main(string[] args)
        {
            Spectrun spectrum = new Spectrun();
            foreach (string color in spectrum)
                Console.WriteLine(color);
            Console.Read();
        }
    }
}
